﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XiaoYuStock.Model;
using XiaoYuStock.Model.Models;
using System.Data;
using System.Data.Entity;
using XiaoYuStock.Model.Request;
using XiaoYuStock.Model.Entities;
using XiaoYuStock.Core;

namespace XiaoYuStockAnalysis.BusinessLogic.Business
{
    public class ShareListDataBusiness
    {

       

        public bool SynShare(ShareList model)
        {
            if (model == null)
                throw new ArgumentNullException("model");

            ShareContext dbContext = new ShareContext();
            var query = from a in dbContext.ShareList
                        where a.ShareCode == model.ShareCode
                        select a;
            if (!query.Any())
            {
                model.PinyinCode = SpellCode.GetSpellCode(model.ShareName.Trim());
                model.Flag = 0;
                dbContext.Entry(model).State = EntityState.Added;
                return dbContext.SaveChanges() > 0;
            }

            if (query.Count() > 0)
            {
                query.ToList().ForEach(s =>
                {
                    s.Status = (int)ShareState.Deleted;
                    model.Flag = 1;
                    s.ModifyDate = DateTime.Now;
                });
            }
            var current = query.FirstOrDefault();
            current.ShareName = model.ShareName;
            current.CategoryCode = model.CategoryCode;
            current.CirculateCapital = model.CirculateCapital;
            current.MarketDate = model.MarketDate;
            current.ModifyDate = DateTime.Now;
            if (current.ShareName != model.ShareName)
            {
                current.Remark = current.Remark + "旧名称:" + current.ShareName+"|";
            }

            current.ShareSubType = model.ShareSubType;
            current.ShareType = model.ShareType;
            current.Status = model.Status;
            current.TotalCapital = model.TotalCapital;
            current.Url = model.Url;
            dbContext.Entry(current).State = EntityState.Modified;
            return dbContext.SaveChanges() > 0;
        }
        public IEnumerable<ShareList> GetList(ShareRequest request,out int totalRecord)
       {
           request = request ?? new ShareRequest();

           using (var dbContext = new ShareContext())
           {

               IQueryable<ShareList> query = dbContext.ShareList;
               if (!string.IsNullOrWhiteSpace(request.ShareCode))
               {
                   query = query.Where(m => m.ShareCode.Contains(request.ShareCode));
               }
               if (!string.IsNullOrWhiteSpace(request.ShareName))
               {
                   query = query.Where(m => m.ShareName.Contains(request.ShareName));
               }
               if (request.ShareStatus.HasValue)
               {
                   query = query.Where(m => m.Status == request.ShareStatus.Value);
               }
               else
               {
                   query = query.Where(m => m.Status != (int)ShareState.Deleted);
               }
               if (request.Flag.HasValue)
               {
                   query = query.Where(m => m.Flag == request.Flag.Value);
               }
               totalRecord = query.Count();
               return query.OrderByDescending(u => u.ShareCode).ToPagedList(request.PageIndex, request.PageSize);
           }
       }

        public ShareList GetFirst(ShareRequest request = null)
        {
            request = request ?? new ShareRequest();

            var dbContext = new ShareContext();
           
                IQueryable<ShareList> query = dbContext.ShareList;
                if (!string.IsNullOrWhiteSpace(request.ShareCode))
                {
                    query = query.Where(m => m.ShareCode.Contains(request.ShareCode)|| m.ShareName.Contains(request.ShareCode));
                }
            return query.FirstOrDefault();
            
        }


        public bool Update(ShareList model)
        {
            if (model == null)
                throw new ArgumentNullException("model");
            if (string.IsNullOrWhiteSpace(model.ShareCode))
                throw new ArgumentNullException("ShareCode");

            ShareContext dbContext = new ShareContext();
            var current = dbContext.ShareList.FirstOrDefault(m => m.Id == model.Id);
            if (current != null)
            {
                current.CategoryCode = model.CategoryCode;
                current.CirculateCapital = model.CirculateCapital;
                current.Flag = model.Flag;
                current.MarketDate = model.MarketDate;
                current.ModifyDate = DateTime.Now;
                current.Remark = model.Remark;
                current.ShareName = model.ShareName;
                current.ShareSubType = model.ShareSubType;
                current.ShareType = model.ShareType;
                current.Status = model.Status;
                current.TotalCapital = model.TotalCapital;
                current.Url = model.Url;
                dbContext.Entry(current).State = EntityState.Modified;
                return dbContext.SaveChanges() > 0;
               
            }
            return false;
        }



     


    }
}
